package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ProjectName duolaimall
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/26 15:11
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;


    @Override
    public OrderListResponse orderList(OrderListRequest request) {

        // 最终返回的对象
        OrderListResponse orderListResponse = new OrderListResponse();

        PageHelper.startPage(request.getPage(), request.getSize());

        // 没见到排序，先不管了

        Example orderExample = new Example(Order.class);
        Example.Criteria criteria = orderExample.createCriteria();
        criteria.andEqualTo("userId", request.getUserId());
        criteria.andNotEqualTo("status", OrderConstants.DELETED_ORDER_STATUS);
        orderExample.setOrderByClause("order_Id DESC");
        List<Order> orders = orderMapper.selectByExample(orderExample);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        orderListResponse.setTotal(orderPageInfo.getTotal());

        List<OrderDetailInfo> orderDetailInfos = orderConverter.order2detail(orders);
        for (int i = 0; i < orderDetailInfos.size(); i++) {

            String orderId = orderDetailInfos.get(i).getOrderId();

            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfos.get(i).setOrderItemDto(orderItemDtos);

            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfos.get(i).setOrderShippingDto(orderShippingDto);
        }

        orderListResponse.setData(orderDetailInfos);
        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {

        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);

        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);

        // 这里应该不是真正去member表里查username吧？
        orderDetailResponse.setUserName(order.getBuyerNick());

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        orderDetailResponse.setGoodsList(orderItemDtos);


        return orderDetailResponse;
    }
}
